package edu.hubu.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import net.moon.jpa.model.BaseEntity;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author moonlan
 * @date 2020/11/2 下午4:55
 */
@Entity
@Table(name = "permission")
//逻辑删除注解，删除sql变成了update
@SQLDelete(sql = "update permission set permission_is_deleted = true where permission_id = ?")
//where条件带上了逻辑删除条件
@Where(clause = "permission_is_deleted = false")
@ApiModel("权限")
@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler", "fieldHandler"})
public class Permission extends BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "snowflakeId")
    @GenericGenerator(name = "snowflakeId", strategy = "edu.hubu.commons.SnowflakeId",
                      parameters = {@org.hibernate.annotations.Parameter(name = "prefix", value = "PERMISSION")})
    @Column(name = "permission_id")
    @ApiModelProperty(value = "permission的id", position = 1)
    private String permissionId;

    @Column(name = "permission_name")
    @ApiModelProperty(value = "权限名称名字", position = 2)
    private String permissionName;

    @Column(name = "permission_is_deleted")
    @ApiModelProperty(value = "权限是否被删除", position = 3)
    private boolean permissionIsDeleted;

    @Column(name = "permission_is_forbidden")
    @ApiModelProperty(value = "权限是否被禁用", position = 4)
    private boolean permissionIsForbidden;

    @ManyToMany(mappedBy = "permissions", fetch = FetchType.LAZY)
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private List<Role> roles = new ArrayList<>();

    public Permission(String permissionId, String permissionName, boolean permissionIsDeleted, boolean permissionIsForbidden, List<Role> roles) {
        this.permissionId = permissionId;
        this.permissionName = permissionName;
        this.permissionIsDeleted = permissionIsDeleted;
        this.permissionIsForbidden = permissionIsForbidden;
        this.roles = roles;
    }

    public Permission() {
    }

    public static PermissionBuilder builder() {
        return new PermissionBuilder();
    }

    public String getPermissionId() {
        return this.permissionId;
    }

    public String getPermissionName() {
        return this.permissionName;
    }

    public boolean isPermissionIsDeleted() {
        return this.permissionIsDeleted;
    }

    public boolean isPermissionIsForbidden() {
        return this.permissionIsForbidden;
    }

    public List<Role> getRoles() {
        return this.roles;
    }

    public void setPermissionId(String permissionId) {
        this.permissionId = permissionId;
    }

    public void setPermissionName(String permissionName) {
        this.permissionName = permissionName;
    }

    public void setPermissionIsDeleted(boolean permissionIsDeleted) {
        this.permissionIsDeleted = permissionIsDeleted;
    }

    public void setPermissionIsForbidden(boolean permissionIsForbidden) {
        this.permissionIsForbidden = permissionIsForbidden;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    public static class PermissionBuilder {
        private String permissionId;
        private String permissionName;
        private boolean permissionIsDeleted;
        private boolean permissionIsForbidden;
        private List<Role> roles;

        PermissionBuilder() {
        }

        public PermissionBuilder permissionId(String permissionId) {
            this.permissionId = permissionId;
            return this;
        }

        public PermissionBuilder permissionName(String permissionName) {
            this.permissionName = permissionName;
            return this;
        }

        public PermissionBuilder permissionIsDeleted(boolean permissionIsDeleted) {
            this.permissionIsDeleted = permissionIsDeleted;
            return this;
        }

        public PermissionBuilder permissionIsForbidden(boolean permissionIsForbidden) {
            this.permissionIsForbidden = permissionIsForbidden;
            return this;
        }

        public PermissionBuilder roles(List<Role> roles) {
            this.roles = roles;
            return this;
        }

        public Permission build() {
            return new Permission(permissionId, permissionName, permissionIsDeleted, permissionIsForbidden, roles);
        }

        public String toString() {
            return "Permission.PermissionBuilder(permissionId=" + this.permissionId + ", permissionName=" + this.permissionName + ", permissionIsDeleted=" + this.permissionIsDeleted + ", permissionIsForbidden=" + this.permissionIsForbidden + ", roles=" + this.roles + ")";
        }
    }
}
